<?php

global $SITEURL;
global $EDLANG;

$mld = MLDnewsletter::getInstance();    
if (isset($_POST['adminsubscribe'])) {
    $result = subscribe($_POST['subscriber']['name'], $_POST['subscriber']['email'], false);
    display_message($result[0], $result[1]);
} elseif (isset($_POST['unsubscribe'])) {
    $result = unsubscribe($_POST['subscriber']['email']);
    display_message($result[0], $result[1]);
} elseif (isset($_GET['remove'])) {
    $result = unsubscribe($_GET['remove']);
    display_message($result[0], $result[1]);
}
?>

<h3 class="floated"><?php i18n('mld-newsletter/MANAGESUBSCRIBERS'); ?></h3>
<?php $style = (isset($_POST['importcsv_submit'])) ? '' : 'display:none;'; ?>
<div class="edit-nav clearfix">
    <a id="importcsv" href="#" <?php if($style == '') { echo 'class="current"'; } ?>><?php i18n('mld-newsletter/IMPORTCSV'); ?></a>      
</div>    
<div id="importcsv-form" style="<?php echo $style; ?>">
    <form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post">
        <input type="text" id="import_file" name="import_file" class="text" />
        <input type="button" class="submit" id="importcsv_browse" name="importcsv_browse" value="<?php i18n('mld-newsletter/BROWSE'); ?>" />

        <script type="text/javascript">
            $(function() { 
                $('#importcsv_browse').click(function(e) {
                    e.preventDefault();
                    window.open('<?php echo $SITEURL; ?>admin/filebrowser.php?type=all&CKEditorFuncNum=0&langCode=en&returnid=import_file', 'importcsv_browse_window', 'width=750,height=500');
                });
            });
        </script>

        <br /><br />
        <input type="submit" class="submit" id="importcsv_submit" name="importcsv_submit" value="<?php i18n('mld-newsletter/IMPORT'); ?>" style="margin-right: 4px;"/>
        <?php i18n('mld-newsletter/OR'); ?>
        <a class="cancel" href="<?php echo $_SERVER['SCRIPT_NAME']; ?>?id=mld-newsletter&subscribers"><?php i18n('ASK_CANCEL'); ?></a>
    </form>

    <script type="text/javascript">
        $('#importcsv').live("click", function($e) {
            $e.preventDefault();
            $("#importcsv-form").slideToggle();
            $(this).toggleClass('current');
        });
        $("#importcsv-form .cancel").live("click", function($e) {
            $e.preventDefault();
            $("#importcsv-form").slideUp();
            $('#importcsv').toggleClass('current');
        });
    </script>                

    <?php if(isset($_POST['importcsv_submit']) && file_exists('../data/uploads/'.pathinfo($_POST['import_file'], PATHINFO_BASENAME))): ?>
    <form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post">
        <input type="hidden" name="csvfile" value="<?php echo $_POST['import_file']; ?>" />
        <table>
            <caption><?php i18n('mld-newsletter/TH_CSV_DESC'); ?></caption>
            <tr>
                <th><?php i18n('mld-newsletter/TH_COLUMN'); ?></th>
                <th><?php i18n('mld-newsletter/TH_DATAFIRSTROW'); ?></th>
                <th><?php i18n('mld-newsletter/TH_DATASECONDROW'); ?></th>
                <th><?php i18n('mld-newsletter/TH_ISNAME'); ?></th>
                <th><?php i18n('mld-newsletter/TH_ISEMAIL'); ?></th>
            </tr>

            <?php            
            $row = array();
            if (($handle = fopen($_POST['import_file'], "r")) !== FALSE) {
                $i = 1;
                while (($data = fgetcsv($handle, 2000, ",")) !== FALSE) {
                    $row[$i] = $data;                    
                    if($i >= 2) { break; }
                    $i++;
                }
                fclose($handle);                       
                for ($c = 0; $c < count($row[1]); $c++) {
                    echo '<tr><td>'.(string) $c.'</td>';
                    echo '<td>'.$row[1][$c].'</td>';
                    echo '<td>'.$row[2][$c].'</td>';
                    echo '<td><input type="radio" name="isname" value="'.(string) $c.'" /></td>';
                    echo '<td><input type="radio" name="isemail" value="'.(string) $c.'" /></td></tr>';
                }                
            } else {
                display_message('error', i18n_r('mld-newsletter/IMPORTFILE_PROBLEM'));
            }
            ?>

        </table>            
        <input type="submit" class="submit" name="importcsv_final" value="<?php i18n('mld-newsletter/IMPORT'); ?>" />
    </form>
    <?php endif; ?>
    <?php
    if(isset($_POST['import_file']) && !file_exists('../data/uploads/'.pathinfo($_POST['import_file'], PATHINFO_BASENAME))) {
        display_message('error', i18n_r('mld-newsletter/IMPORTFILE_PROBLEM'));
    }
    if(isset($_POST['importcsv_final'])) {
        //import csv addresses            
        $col_name = (int) $_POST['isname'];
        $col_email = (int) $_POST['isemail'];
        $result = array();
        if (($handle = fopen($_POST['csvfile'], "r")) !== FALSE) {
                while (($data = fgetcsv($handle, 2000, ",")) !== FALSE) {                                                                                  
                    $result[] = subscribe($data[$col_name], $data[$col_email], false);                        
                }
                fclose($handle);                                          
            } else {
                display_message('error', i18n_r('mld-newsletter/IMPORTFILE_PROBLEM'));
            }            
    }

    ?>

    <hr style="margin-top: 10px;"/>
</div>    
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post" class="mldsubscribersform" >
    <p><label class="mldlabel"><?php i18n('mld-newsletter/NAME'); ?>:</label>
        <input type="text" class="text" name="subscriber[name]" value=""/></p>        
    <p><label class="mldlabel"><?php i18n('mld-newsletter/EMAIL'); ?>:</label>
        <input type="text" class="text" name="subscriber[email]" value=""/></p>
    <p><input type="submit" class="submit" value="<?php i18n('mld-newsletter/SUBSCRIBE'); ?>" id="adminsubscribe" name="adminsubscribe" />
        <input type="submit" class="submit" value="<?php i18n('mld-newsletter/UNSUBSCRIBE'); ?>" id="unsubscribe" name="unsubscribe" /></p>
</form><br />

<table id="subscribers" class="edittable highlight">
    <tr>
        <th><?php i18n('mld-newsletter/NAME'); ?></th>
        <th><?php i18n('mld-newsletter/EMAIL'); ?></th>
        <th><?php i18n('mld-newsletter/STATUS'); ?></th>
        <th></th>
    </tr>
<?php
//get data
$xml = $mld->getXmlData('subscribers');
foreach ($xml as $entry) {
    echo '<tr><td>' . $entry->name . '</td>';
    echo '<td>' . $entry->email . '</td>';
    if ($entry->status == 'true') {
        $status = 'true';
    } else {
        $status = 'false';
    }
    echo '<td><img alt="" src="../plugins/mld-newsletter/images/' . $status . '.png" /></td>';
    echo '<td class="delete"><a href="' . $_SERVER['REQUEST_URI'] . '&remove=' . $entry->email . '">×</a></td></tr>';
}
?>
</table>